package org.spbsu.apco.common.data.impl;

import java.io.Serializable;
import java.util.Random;

/**
 * User: ortemij
 * Date: 07.01.13
 * Time: 14:58
 */
public class Matrix implements Serializable {

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    private double[][] data;
    private int n;
    private int m;

    public Matrix(int n, int m) {
        this.n = n;
        this.m = m;
        this.data = new double[n][m];
    }

    public void initRandom() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                data[i][j] = RANDOM.nextDouble();
            }
        }
    }

    public double[] getRow(int i) {
        return data[i];
    }

    public void set(int i, int j, double v) {
        data[i][j] = v;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                sb.append(String.format("%.4f", data[i][j])).append(" ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}
